<?php
class Pencarian_grid extends CI_Controller {

    function __construct()
    {
            parent::__construct();	
            $this->load->library('form_validation');
            $this->load->helper('generate_pdf');
            if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get"))
                @date_default_timezone_set(@date_default_timezone_get());
    }
	
	
    function form_pencarian_grid()
    {
	
	$pilih_cari = $this->input->post('pencarian');
	
	// Setting Error Message
	$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
	$this->form_validation->set_message('numeric', 'Kolom %s harus diisi dengan karakter angka saja, desimal menggunakan titik (.)');
	$this->form_validation->set_message('is_unique', 'Data kolom %s sudah digunakan!!');
	
	// Setting Delimiter
	$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
	
	if($pilih_cari == 1){//list program
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	}else if($pilih_cari == 2){//list kegiatan
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('program', 'Program', 'callback_cek_dropdown');
	}else if($pilih_cari == 3){//list sasaran strategis (program)
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('program', 'Program', 'callback_cek_dropdown');
	}else if($pilih_cari == 4){//list sasaran strategis (kegiatan)
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('program', 'Program', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('kegiatan', 'Kegiatan', 'callback_cek_dropdown');
	}else if($pilih_cari == 5){//list indikator (program)
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('program', 'Program', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('sasaran_prog', 'Sasaran Strategis (Program)', 'callback_cek_dropdown');
	}else if($pilih_cari == 6){//list indikator (kegiatan)
	    $this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('program', 'Program', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('kegiatan', 'Kegiatan', 'callback_cek_dropdown');
	    $this->form_validation->set_rules('sasaran_keg', 'Sasaran Strategis (Kegiatan)', 'callback_cek_dropdown');
	}
	
	if ($this->form_validation->run() == FALSE){
	    
	    //load model
	    $this->load->model('anggaran_model');
	    
	    //load query
	    $list_anggaran = $this->anggaran_model->get_anggaran();
	    
	    //variable inisializition
	    $data['options_anggaran'] = array('-- Pilih Tahun Anggaran --');
	    
	    //load query result
	    foreach($list_anggaran->result() as $row){
		$data['options_anggaran'][$row->IDANGGARAN] = $row->TAHUN;
	    }
	    
	    //load view
	    $data['content'] = $this->load->view('form_pencarian_grid',$data,true);
	    $this->load->view('main',$data);
	   
	}else{
	    
	    if($pilih_cari == 1){//list program
	    $anggaran = $this->input->post('tahun');
	    redirect('program/program/'.$anggaran);
	    }else if($pilih_cari == 2){//list kegiatan
		$anggaran = $this->input->post('tahun');
		$program = $this->input->post('program');
		redirect('kegiatan/kegiatan/'.$program);
	    }else if($pilih_cari == 3){//list sasaran strategis (program)
		$anggaran = $this->input->post('tahun');
		$program = $this->input->post('program');
		redirect('sasaran_program/rinci/'.$program);
	    }else if($pilih_cari == 4){//list sasaran strategis (kegiatan)
		$anggaran = $this->input->post('tahun');
		$program = $this->input->post('program');
		$kegiatan = $this->input->post('kegiatan');
		redirect('sasaran_kegiatan/rinci/'.$kegiatan);
	    }else if($pilih_cari == 5){//list indikator (program)
		$sasaran_prog = $this->input->post('sasaran_prog');
		redirect('indikator_program/rinci/'.$sasaran_prog);
	    }else if($pilih_cari == 6){//list indikator (kegiatan)
		$sasaran_keg = $this->input->post('sasaran_keg');
		redirect('indikator_kegiatan/rinci/'.$sasaran_keg);
	    }
	    
	}
    }// end of function
	
	function cek()
	{
		if ($this->cek_validasi() == FALSE){
			$this->form_pencarian();
		}
		else{
			$this->cari();
		}
	}
	
	
	
	function get_program(){
		if ($_POST['tahun']!='' && $_POST['tahun']!='0') //$_POST['first_select'] is the post variable you sent via AJAX call
        {
        	$tahun = $_POST['tahun'];
			$this->load->model('pencarian_model');
			$data = $this->pencarian_model->get_program($tahun);
			$options.= '<option value="0">-- Pilih Semua --</option>';
			foreach ($data->result() as $row)
				{
					$options.= '<option value="'.$row->IDPROGRAM.'" class="dynamic">'.$row->DESKRIPSI.'</option>';
				}        
			echo $options;
        }
		else {
		echo '<option value="0">-- Pilih Semua --</option>';}
	}
	
	function get_sasaran_program(){
		if ($_POST['program']!='' && $_POST['program']!='0') //$_POST['first_select'] is the post variable you sent via AJAX call
        {
        	$program = $_POST['program'];
			$this->load->model('pencarian_model');
			$data = $this->pencarian_model->get_sasaran($program);
			$options.= '<option value="0">-- Pilih Semua --</option>';
			foreach ($data->result() as $row)
				{
					$options.= '<option value="'.$row->IDSASARAN_PROG.'" class="dynamic">'.$row->DESKRIPSI.'</option>';
				}        
			echo $options;
        }
		else {
		echo '<option value="0">-- Pilih Semua --</option>';}
	}
	
	function get_kegiatan(){
		if ($_POST['program']!='' && $_POST['program']!='0') //$_POST['first_select'] is the post variable you sent via AJAX call
        {
        	$program = $_POST['program'];
			$this->load->model('pencarian_model');
			$data = $this->pencarian_model->get_kegiatan($program);
			$options.= '<option value="0">-- Pilih Semua --</option>';
			foreach ($data->result() as $row)
				{
					$options.= '<option value="'.$row->IDKEGIATAN.'" class="dynamic">'.$row->DESKRIPSI.'</option>';
				}        
			echo $options;
        }
		else {
		echo '<option value="0">-- Pilih Semua --</option>';}
	}
	
	function get_sasaran_keg(){
		if ($_POST['kegiatan']!='' && $_POST['kegiatan']!='0') //$_POST['first_select'] is the post variable you sent via AJAX call
        {
        	$kegiatan = $_POST['kegiatan'];
			$this->load->model('pencarian_model');
			$data = $this->pencarian_model->get_sasaran_keg($kegiatan);
			$options.= '<option value="0">-- Pilih Semua --</option>';
			foreach ($data->result() as $row)
				{
					$options.= '<option value="'.$row->IDSASARAN_KEG.'" class="dynamic">'.$row->DESKRIPSI.'</option>';
				}        
			echo $options;
        }
		else {
		echo '<option value="0">-- Pilih Semua --</option>';}
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('tahun', 'Tahun Anggaran', 'callback_cek_dropdown');
			$this->form_validation->set_rules('target_prog', 'Target Indikator(Program)', 'required|numeric');
			$this->form_validation->set_rules('real_prog', 'Realisasi Indikator(Program)', 'required|numeric');
			$this->form_validation->set_rules('target_keg', 'Target Indikator(Kegiatan)', 'required|numeric');
			$this->form_validation->set_rules('real_keg', 'Realisasi Indikator(Kegiatan)', 'required|numeric');
			
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('numeric', 'Kolom %s harus diisi dengan karakter angka saja, desimal menggunakan titik (.)');
			$this->form_validation->set_message('is_unique', 'Data kolom %s sudah digunakan!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	
	function cek_dropdown($value){
		if($value === '0'){
			$this->form_validation->set_message('cek_dropdown', 'Kolom %s harus diisi!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

  function format_tanggal($tanggal_x){
		$date = explode ("-",$tanggal_x);
		if($date[0]!='' && $date[1]!='' && $date[2]!=''){
		$tahun = $date[0];
		$tanggal = $date[2]; 
		switch ($date[1]) {
			case '01':
				$bulan='Januari';
				break;
			case '02':
				$bulan='Februari';
				break;
			case '03':
				$bulan='Maret';
				break;
			case '04':
				$bulan='April';
				break;
			case '05':
				$bulan='Mei';
				break;
			case '06':
				$bulan='Juni';
				break;
			case '07':
				$bulan='Juli';
				break;
			case '08':
				$bulan='Agustus';
				break;
			case '09':
				$bulan='September';
				break;
			case '10':
				$bulan='Oktober';
				break;
			case '11':
				$bulan='November';
				break;
			case '12':
				$bulan='Desember';
				break;		
		}
		$tanggal_indo = $tanggal." ".$bulan." ".$tahun;
		}else{
		$tanggal_indo = '';
		}		
		return $tanggal_indo;
	}
}//end of Class
?>